<?php
add_action( 'admin_enqueue_scripts', 'nino_contact_enqueue');
function nino_contact_enqueue($hook) {
	
	if (isset($_GET['page']) && $_GET['page'] == 'nino_contact_form_admin') {
		wp_enqueue_script('nino-contact-form-script', NINO_CONTACT_URL . 'includes/assets/js/nino-contact-form.js', array('jquery','jquery-ui-core','jquery-ui-sortable', 'jquery-ui-accordion'));
		
		// in javascript, object properties are accessed as ajax_object.ajax_url, ajax_object.we_value
		wp_localize_script( 'nino-contact-form-script', 'nino_contact_ajax_object',
	            array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
	}
}

add_action("wp_ajax_nino_test_send_mail", "nino_contact_test_send_mail");

function nino_contact_test_send_mail() {
	
	// Start output buffering to grab smtp debugging output
	ob_start();
	
	global $phpmailer;
	
	// Make sure the PHPMailer class has been instantiated 
	// (copied verbatim from wp-includes/pluggable.php)
	// (Re)create it, if it's gone missing
	if ( !is_object( $phpmailer ) || !is_a( $phpmailer, 'PHPMailer' ) ) {
		require_once ABSPATH . WPINC . '/class-phpmailer.php';
		require_once ABSPATH . WPINC . '/class-smtp.php';
		$phpmailer = new PHPMailer( true );
	}
	
	$to = $_POST['email'];

	$subject = "Nino Contact Form : Test mail";
	$message = "This is a test email generated by Nino Contact Form WordPress plugin.";
	
	$sitename = strtolower( $_SERVER['SERVER_NAME'] );
	if ( substr( $sitename, 0, 4 ) == 'www.' ) {
		$sitename = substr( $sitename, 4 );
	}

	$default_from = 'wordpress@' . $sitename;
	// Send the test mail
	$result = wp_mail($to, $subject, $message, $default_from);
	
	// Strip out the language strings which confuse users
	//unset($phpmailer->language);
	// This property became protected in WP 3.2
	
	// Grab the smtp debugging output
	$smtp_debug = ob_get_clean();
	
	if ($result) {
		echo '1';
	} else {
		?>
<div id="message" class="updated fade"><p><strong><?php _e('Test Message Sent', 'wp_mail_smtp'); ?></strong></p>
<p><?php _e('The result was:', 'wp_mail_smtp'); ?></p>
<pre><?php var_dump($result); ?></pre>
<p><?php _e('The full debugging output is shown below:', 'wp_mail_smtp'); ?></p>
<pre><?php var_dump($phpmailer); ?></pre>
<p><?php _e('The SMTP debugging output is shown below:', 'wp_mail_smtp'); ?></p>
<pre><?php echo $smtp_debug ?></pre>
</div>
		<?php
	}
	die;
}